package com.whc.controller;

import com.whc.entity.Category;
import com.whc.result.Result;
import com.whc.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 韦红材
 * @ClassName CategoryContoller
 * @data 2024/11/26 22:01
 * @Version V1.0
 **/
@RestController
@RequestMapping("/client/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @ApiOperation("查询所有分类")
    @GetMapping("/selectAll")
    public Result<List<Category>> selectAll(){
        List<Category> categories = categoryService.selectAll();
        return Result.success(categories);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/selectOne")
    @PreAuthorize("hasAuthority('user')")
    public Result<Category> selectOne(long id){
        Category category = categoryService.selectOne(id);
        return Result.success(category);
    }

    @ApiOperation("修改分类")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('admin')")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return Result.success();
    }
    @ApiOperation("添加分类")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('admin')")
    public Result add(String CategoryName){
        categoryService.add(CategoryName);
        return Result.success();
    }
    @ApiOperation("删除分类")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('admin')")
    public Result delete(@PathVariable long id){
        categoryService.delete(id);
        return Result.success();
    }
}
